﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace MockupDesigner.AppItems.Commands
{
    public class UngroupCommand : ICommand
    {

        public CommandType CommandType
        {
            get { return CommandType.Undoable; }
        }

        private DesignerCanvas designerCanvas;
        private IList<GroupCommand> groupCommands = new List<GroupCommand>(); // One GroupCommand / Group

        internal IDictionary<DesignerItem, IList<DesignerItem>> Groups { get; set; } // map Group and the children for the Cancel()

        public UngroupCommand()
        {
            Groups = new Dictionary<DesignerItem, IList<DesignerItem>>();
        }

        public void Execute(object target, params object[] parameters)
        {
            if (!(target is DesignerCanvas))
            {
                throw new InvalidCastException("The target must be a DesignerCanvas");
            }
            this.designerCanvas = (DesignerCanvas)target;

            Redo();
        }

        public void Redo()
        {
            if (Groups.Count == 0) // Command executed directly (e.g not by a Cancel in GroupCommand)
            {
                foreach (DesignerItem item in this.designerCanvas.SelectionService.CurrentSelection.OfType<DesignerItem>())
                {
                    if (item.IsGroup && item.ParentID == Guid.Empty)
                        Groups.Add(item, null); // children will be given later
                }
            }
            for (int i = 0; i < Groups.Count; i++)
            {
                DesignerItem currentGroup = Groups.Keys.ElementAt(i);
                IList<DesignerItem> children = null;
                if (Groups[currentGroup] == null) // idem
                {
                    children = (from child in this.designerCanvas.SelectionService.CurrentSelection.OfType<DesignerItem>()
                                where child.ParentID == currentGroup.ID
                                select child).ToList();
                    Groups[currentGroup] = new List<DesignerItem>(children);
                }
                else
                {
                    children = new List<DesignerItem>(Groups[currentGroup]);
                }
                foreach (DesignerItem child in children)
                    child.ParentID = Guid.Empty;

                this.designerCanvas.SelectionService.RemoveFromSelection(currentGroup);
                this.designerCanvas.Children.Remove(currentGroup);
                this.designerCanvas.UpdateZIndex();
            }
        }

        public void Undo()
        {
            if (groupCommands.Count == 0) // first Cancel
            {
                foreach (DesignerItem group in Groups.Keys)
                {
                    GroupCommand groupCommand = new GroupCommand() { Group = group };
                    groupCommand.Items = Groups[group];
                    groupCommands.Add(groupCommand);
                }
            }
            foreach (GroupCommand groupCommand in groupCommands)
            {
                groupCommand.Execute(this.designerCanvas,null);
            }
        }


    }
}
